//
//  VS+Controls.swift
//  PMS
//
//  Created by 何海涛 on 16/7/29.
//  Copyright © 2016年 途家. All rights reserved.
//

import Foundation

// for localization
extension VS {
    /// localization margin
    @nonobjc public static let marginForPunctuation : [String: CGFloat] = ["":VS.margin5, "zh":0]
}


// control standard for section view.
extension VS {
    
    public class func makeSeparator(_ color: UIColor = VS.sepPageGray) -> UIView {
        let view = UIView()
        view.backgroundColor = VS.bgGray
        view.setBorder(color: color)
        return view
    }
    
    public class func makeSection() -> UILabel {
        return makeItemTitle()
    }
    
    public class func makeHeader() -> UILabel{
        let title = UILabel()
        title.textAlignment = .left
        title.backgroundColor = CommonColors.c_f7f9fb
        title.lineBreakMode = .byTruncatingMiddle
        title.font = VS.font14
        title.textColor = CommonColors.c_333333
        return title
    }
    
    public class func makeTitle() -> UILabel {
        let title = UILabel()
        title.textAlignment = .left
        title.lineBreakMode = .byTruncatingMiddle
        title.font = VS.font14
        title.textColor = VS.txtMain
        return title
    }
    
    public class func makeleftTitle() -> UILabel {
        let title = UILabel()
        title.textAlignment = .left
        title.lineBreakMode = .byTruncatingMiddle
        title.font = VS.font14
        title.textColor = CommonColors.c_333333
        return title
    }
    
    
    public class func makeItemTitle() -> UILabel {
        let title = UILabel()
        title.textAlignment = .left
        title.lineBreakMode = .byTruncatingMiddle
        title.font = VS.font14
        title.textColor = CommonColors.c_999999
        return title
    }
    
    public class func makeItemValue() -> UILabel {
        let title = UILabel()
        title.textAlignment = .left
        title.lineBreakMode = .byTruncatingMiddle
        title.font = VS.font14
        title.textColor = CommonColors.c_333333
        return title
    }
    
    public class func makeLabel(_ font: UIFont, _ color: UIColor, _ algin : NSTextAlignment = .left) -> UILabel{
        let title = UILabel()
        title.textAlignment = algin
        title.lineBreakMode = .byTruncatingMiddle
        title.font = font
        title.textColor = color
        return title
    }
    
    /**
     Init new button.
     
     - parameter font:   Title label font.
     - parameter title:  Button title for .Normal.
     - parameter colors: See <b>MutexColors</b>
     
     - returns: new button.
     */
    public class func makeButton(_ font: UIFont, _ title: String?, _ colors : MutexColor) -> UIButton {
        let btn = UIButton()
        btn.titleLabel?.font = font
        btn.setTitle(title, for: UIControl.State())
        btn.setMutexColor(colorPairs: colors)
        return btn
    }
    
    /**
     Init new checkable button
     
     - parameter font:   Title label font.
     - parameter title:  Button title for .Normal.
     - parameter colors: See <b>MutexColors</b>
     
     - returns: new button
     */
    public class func makeCheckButton(_ font: UIFont, _ title: String?, _ colors : MutexColor, _ align : UIControl.ContentHorizontalAlignment = .left) -> UIButton {
        let btn = makeButton(font, title, colors)
        btn.setImage(UIImage(named: "selected_icon"), for: .selected)
        btn.setImage(UIImage(named: "unselected_icon"), for: UIControl.State())
        btn.contentHorizontalAlignment = align
        return btn
    }
    
    /**
     Init new iamgebutton
     
     - parameter font:   Title label font.
     - parameter title:  Button title for .Normal.
     - parameter imageName: imageName
     
     - returns: new button
     */
    public class func makeButtonWithLeftImage(_font:UIFont,title:String?,imageName:String) -> UIButton {
        let btn = UIButton()
        let contentView = UIView()
        btn.addSubview(contentView)
        let leftImageView = UIImageView()
        leftImageView.isUserInteractionEnabled = true
        leftImageView.image = UIImage(named:imageName)
        contentView.addSubview(leftImageView)
        let rightTitle = VS.makeLabel(VS.font14, CommonColors.c_fd8238)
        rightTitle.text = title
        contentView.addSubview(rightTitle)
        leftImageView.snp.makeConstraints { (make) in
            make.left.equalTo(contentView)
            make.centerY.equalTo(contentView)
//            make.left.equalTo(contentView.snp.left)
//            make.centerY.equalTo(contentView)
        }
        rightTitle.snp.makeConstraints { (make) in
            make.left.equalTo(leftImageView.snp.right).offset(10)
            make.centerY.equalTo(contentView)
            make.right.equalTo(contentView)
        }
        contentView.snp.makeConstraints { (make) in
            make.center.equalTo(btn)
        }
        return btn
    }
    
    /**
     Init new help button
     
     - returns: new button
     */
    public class func makeHelpButton() -> UIButton {
        let btn = UIButton()
        btn.setImage(UIImage(named: "doubt"), for: UIControl.State())
        btn.snp.makeConstraints { (make) in
            make.width.height.equalTo(15)
        }
        return btn
    }
    
    /**
     create two labels for item title and item value.
     
     - parameter container: parent view for title label and value label
     - parameter name:           title
     - parameter nameConstraint: title constraint
     
     - returns: title label and value label.
     */
//    public class func makeTitleAndValuePair(_ container: UIView, name: String, nameConstraint: (_ make: ConstraintMaker) -> Void) -> (title: UILabel, value: UILabel) {
//        let title = makeItemTitle()
//        let value = makeItemValue()
//        container.addSubview(title)
//        container.addSubview(value)
//        
//        title.text = name
//        title.snp.updateConstraints(nameConstraint)
//        value.snp.updateConstraints { (make) in
//            make.centerY.equalTo(title)
//            make.left.equalTo(title.snp.right).offsetLocalization(VS.marginForPunctuation)
//            make.right.lessThanOrEqualTo(container).offset(-VS.margin15)
//        }
//        return (title, value)
//    }
//    /**
//     create two labels for item title and item value.
//     
//     - parameter container:      parent view for title label and value label
//     - parameter name:           title
//     - parameter nameConstraint: localization constraint dictionary.
//     
//     - returns: title label and value label.
//     */
//    public class func makeTitleAndValuePair(_ container: UIView, name: String, nameConstraint: LocalizationConstraint) -> (title: UILabel, value: UILabel) {
//        let title = makeItemTitle()
//        let value = makeItemValue()
//        container.addSubview(title)
//        container.addSubview(value)
//        
//        title.text = name
//        title.snp_updateConstraintsLocalization(nameConstraint)
//        value.snp.updateConstraints { (make) in
//            make.centerY.equalTo(title)
//            make.left.equalTo(title.snp.right).offsetLocalization(VS.marginForPunctuation)
//            make.right.lessThanOrEqualTo(container).offset(-VS.margin15)
//        }
//        return (title, value)
//    }
}

// for separater line.
extension UIView {
    
    /**
     add bottom separater line.
     
     - parameter space: offset to UIView's left and right.
     */
    @discardableResult
    public func withBottomSeparater(space : CGFloat = 0, color: UIColor = VS.sepPageGray) -> UIView {
        let sep = VS.makeSeparator(color)
        self.addSubview(sep)
        sep.snp.updateConstraints { (make) in
            make.height.equalTo(CommonDefine.kPixel)
            make.bottom.equalTo(self)
            make.left.equalTo(self).offset(space)
            make.right.equalTo(self).offset(-space)
        }
        return sep
    }
    
    @discardableResult
    public func withTopSeparater(space: CGFloat = 0, color: UIColor = VS.sepPageGray) -> UIView {
        let sep = VS.makeSeparator(color)
        self.addSubview(sep)
        sep.snp.updateConstraints { (make) in
            make.height.equalTo(CommonDefine.kPixel)
            make.top.equalTo(self)
            make.left.equalTo(self).offset(space)
            make.right.equalTo(self).offset(-space)
        }
        return sep
    }
    
    public func withLeftSeparater(multiple: CGFloat = 0.5, color: UIColor = VS.sepPageGray) -> UIView {
        let sep = VS.makeSeparator(color)
        self.addSubview(sep)
        sep.snp.updateConstraints { (make) in
            make.width.equalTo(CommonDefine.kPixel)
            make.left.centerY.equalTo(self)
            make.height.equalTo(self).multipliedBy(multiple)
        }
        return sep
    }
    
    @discardableResult
    public func withRightSeparater(multiple: CGFloat = 0.5, color: UIColor = VS.sepPageGray) -> UIView {
        let sep = VS.makeSeparator(color)
        self.addSubview(sep)
        sep.snp.updateConstraints { (make) in
            make.width.equalTo(CommonDefine.kPixel)
            make.right.centerY.equalTo(self)
            make.height.equalTo(self).multipliedBy(multiple)
        }
        return sep
    }
}

public class TriangleView : UIView {
    
    public init() {
        super.init(frame: CGRect.zero)
        self.backgroundColor = UIColor.clear
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 画和控件重合的1条三角线的颜色
    public var fillColor : UIColor = UIColor.white
    /// 画外面2条三角线的颜色
    public var strokeColor : UIColor = UIColor.black
    public var lineWidth = CommonDefine.kPixel
    
    override public func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext() {
            // clear
            UIColor.clear.setFill()
            context.fill(rect)
            
            // use path to draw triangle
            context.beginPath()
            
            context.setLineWidth(1)
            context.move(to: CGPoint(x: 0, y: 0)) // start point
            fillColor.setStroke()
            context.addLine(to: CGPoint(x: self.w, y: 0))
            context.strokePath()
            
            context.beginPath()
            context.move(to: CGPoint(x: self.w, y: 0)) // start point
            strokeColor.setStroke()
            context.addLine(to: CGPoint(x: self.w/2, y: self.h))
            context.addLine(to: CGPoint(x: 0, y: 0))
            context.strokePath()
            
            // fill
            context.beginPath()
            context.move(to: CGPoint(x: 0, y: 0))
            context.addLine(to: CGPoint(x: self.w, y: 0))
            context.addLine(to: CGPoint(x: self.w/2, y: self.h))
            context.closePath()
            fillColor.setFill()
            context.fillPath()
        }
    }
}

public class LeftTrangleView : UIView {
    
    public init() {
        super.init(frame: CGRect.zero)
        self.backgroundColor = UIColor.clear
    }
    
    required public init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    /// 画和控件重合的1条三角线的颜色
    public var fillColor : UIColor = UIColor.white
    /// 画外面2条三角线的颜色
    public var strokeColor : UIColor = UIColor.black
    public var lineWidth = CommonDefine.kPixel
    public var needCoverBorder = false
    
    override public func draw(_ rect: CGRect) {
        if let context = UIGraphicsGetCurrentContext() {
            // clear
            UIColor.clear.setFill()
            context.fill(rect)
            
            // use path to draw triangle
            if needCoverBorder {
                context.beginPath()
                context.setLineWidth(1)
                context.move(to: CGPoint(x: self.w, y: 0)) // start point
                context.addLine(to: CGPoint(x: self.w, y: self.h))
                context.setStrokeColor(fillColor.cgColor)
                context.strokePath()
            }
            
            context.beginPath()
            context.move(to: CGPoint(x: self.w, y: self.h)) // start point
            context.addLine(to: CGPoint(x: 1, y: self.h/2))
            context.addLine(to: CGPoint(x: self.w, y: 0))
            context.setStrokeColor(strokeColor.cgColor)
            context.strokePath()
            
            // fill
            context.beginPath()
            context.move(to: CGPoint(x: self.w, y: 0))
            context.addLine(to: CGPoint(x: self.w, y: self.h))
            context.addLine(to: CGPoint(x: 1, y: self.h/2))
            context.closePath()
            context.setFillColor(fillColor.cgColor)
            context.fillPath(using:.evenOdd)
        }
    }
}

extension UIView {
    @discardableResult
    public func withTopTriangleView(_ multiple: CGFloat, width: CGFloat = VS.margin11*2, height: CGFloat = VS.margin8) -> TriangleView {
        let triangle = TriangleView()
        triangle.fillColor = UIColor.white
        triangle.strokeColor = VS.sepPageGray
        self.addSubview(triangle)
        triangle.snp.updateConstraints { (make) in
            make.centerX.equalTo(self).multipliedBy(multiple)
            make.top.equalTo(self).offset(-1)
            make.width.equalTo(width)
            make.height.equalTo(height)
        }
        return triangle
    }
    
    @discardableResult
    public func withBottomTriangleView(_ multiple: CGFloat = 1, width: CGFloat = VS.margin11*2, height: CGFloat = VS.margin8) -> TriangleView {
        let triangle = TriangleView()
        triangle.fillColor = UIColor.white
        triangle.strokeColor = VS.sepPageGray
        self.addSubview(triangle)
        triangle.snp.updateConstraints { (make) in
            make.centerX.equalTo(self).multipliedBy(multiple)
            make.top.equalTo(self.snp.bottom).offset(-1)
            make.width.equalTo(width)
            make.height.equalTo(height)
        }
        return triangle
    }
    
    @discardableResult
    public func withLeftTrangleView(container:UIView,
                                    multiple: CGFloat = 1,
                                    widthMultiple: CGFloat = 0.4,
                                    widthMax: CGFloat = 10,
                                    height: CGFloat = 10,
                                    fillColor: UIColor = CommonColors.c_f2f2f2,
                                    strokeColor: UIColor = CommonColors.c_d3d3d3,
                                    hasBorder: Bool = false) -> LeftTrangleView {
        let triangle = LeftTrangleView()
        triangle.fillColor = fillColor
        triangle.strokeColor = strokeColor
        container.addSubview(triangle)
        triangle.snp.updateConstraints { (make) in
            make.centerY.equalTo(self).multipliedBy(multiple)
            make.right.equalTo(self.snp.left).offset(hasBorder ? 1 : 0)
            make.height.equalTo(self.snp.height).multipliedBy(widthMultiple)
            make.height.lessThanOrEqualTo(widthMax)
            make.width.equalTo(height)
        }
        return triangle
    }
}
